//
//  TweetCellView.m
//  TwitterFon
//
//  Created by kaz on 11/2/08.
//  Copyright 2008 naan studio. All rights reserved.
//

#import "TweetCellView.h"
#import "GuaziAppDelegate.h"
#import "TweetImageStore.h"
#import "FriendsTimelineController.h"

@implementation TweetCellView

@synthesize status;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if ( self ) {
    }
    return self;
}

- (void)setStatus:(Status*)value
{
    if (status != value) {
		[status release];
		status = [value retain];
	}
    
    [super setNeedsLayout];
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect
{
    UIColor* textColor;
    UIColor* timestampColor;
    
	UITableViewCell* cell = (UITableViewCell*)self.superview.superview;
	if (cell.selected) {
		textColor       = [UIColor whiteColor];
        timestampColor  = [UIColor lightGrayColor];
	}
	else {
		textColor       = [UIColor blackColor];
        timestampColor  = [UIColor grayColor];
	}
    
    float textFontSize = (status.cellType == TWEET_CELL_TYPE_DETAIL) ? 14 : 13;
    
	[textColor set];
    if (status.cellType == TWEET_CELL_TYPE_NORMAL) {
		[status.user.screenName drawInRect:CGRectMake(0, 0, CELL_WIDTH - DETAIL_BUTTON_WIDTH, TOP) withFont:[UIFont boldSystemFontOfSize:14]];
    }

	[status.text drawInRect:status.textBounds withFont:[UIFont systemFontOfSize:textFontSize]];
	[timestampColor set];
    if (status.cellType != TWEET_CELL_TYPE_NORMAL) {
        NSString *timestamp;
        if ([status.source length]) {
            timestamp = [status.timestamp stringByAppendingFormat:@" 来自%@", [status sourceDescription]];
        }
        else {
            timestamp = status.timestamp;
        }
        [timestamp drawInRect:CGRectMake(0, status.textBounds.size.height + 3, 250, 16) withFont:[UIFont systemFontOfSize:12]];
    }
    else {
        [status.timestamp drawInRect:CGRectMake(0, TOP + status.textBounds.size.height - 1, 250, 16) withFont:[UIFont systemFontOfSize:12]];
    }

    //显示图片
    if ( status.smallPicture && [status.smallPicture length] > 0 ) {
        UIImage* image = [[TweetImageStore sharedInstance] getTweetImage:status.smallPicture square:80.0f delegate:self];
        smallPictureRect = CGRectMake(0, TOP + status.textBounds.size.height + 26, 80, 80);
        [image drawInRect:smallPictureRect];
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ( status.smallPicture && [status.smallPicture length] > 0 ) {
        UITouch* touch = [touches anyObject];
        CGPoint p = [touch locationInView:self];
        if ( p.x > smallPictureRect.origin.x && p.x < smallPictureRect.origin.x + smallPictureRect.size.width &&
             p.y > smallPictureRect.origin.y && p.y < smallPictureRect.origin.y + smallPictureRect.size.height ) {
            UIViewController* controller = [[GuaziAppDelegate getAppDelegate] currentViewController];
            if ( [controller respondsToSelector:@selector(showPicture:)]) {
               UIImage* image = [[TweetImageStore sharedInstance] getTweetImage:status.middlePicture square:200 delegate:self];
                [controller performSelector:@selector(showPicture:) withObject:image];
            }
            else {
                [super touchesBegan:touches withEvent:event];
            }
        }
        else {
            [super touchesBegan:touches withEvent:event];
        }
    }
    else {
        [super touchesBegan:touches withEvent:event];
    }
}


- (void) imageDidGetNewImage:(UIImage*)image forURL:(NSString*)url
{
    if ( [status.smallPicture compare:url] == NSOrderedSame ) {
        smallPictureRect = CGRectMake(0, TOP + status.textBounds.size.height + 26, 80, 80);
        [image drawInRect:smallPictureRect];
    }
    else {
        UIViewController* controller = [[GuaziAppDelegate getAppDelegate] currentViewController];
        if ( [controller isKindOfClass:[FriendsTimelineController class]] ) {
            FriendsTimelineController* friendTimelineController = (FriendsTimelineController*) controller;
            friendTimelineController.middlePicture.image = image;
        }
    }
}


- (void)dealloc {
    [status release];
    [super dealloc];
}


@end
